const KoaAction = require('./koa-action');
const path = require('path');
const sse = require('koa-sse-stream');
const log4js = require('log4js');
log4js.configure({
    appenders: { 'log': { type: 'dateFile', daysToKeep: 15, layout: {
        type: 'pattern',
        pattern: '[%d{yyyy-MM-dd hh:mm:ss}] [%p] %c - %m'
    }, compress: true, filename: path.join(process.cwd(), 'logs', 'log.log') } },
    categories: { default: { appenders: ['log'], level: 'info' } },
    pm2: true,
    pm2InstanceVar: 'INSTANCE_ID'
});
const logger = log4js.getLogger('log');
const app = new KoaAction();
app.use(sse({
    maxClients: 5000,
    pingInterval: 30000,
    matchQuery: 'sse'
}))
process.on('uncaughtException', (err, origin) => {
    logger.error(`捕获的异常: ${err}\n异常的来源: ${origin}`);
});

app.use(async (ctx, next) => {
    await next();
    if (ctx.status === 404) {
        ctx.throw(404, '');
    }
});

app.koa.on('error', (err, ctx) => {
    // 客户端重置或断开连接
    logger.error(ctx.request.url, err);
});

app.run(() => {
    logger.info(' [系统启动成功]');
});
